package cn.com.dao;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.entity.Doctor;


public class DoctorDao {
	//医生的dao文件
		private  SessionFactory sessionFac;
			
		public SessionFactory getSessionFac() {
				return sessionFac;
		}
		public void setSessionFac(SessionFactory sessionFac) {
				this.sessionFac = sessionFac;
		}
		Session session;
		Transaction tr;
	
	//医生登录	
		public Doctor doctorLogin(Doctor doctor){
			session=sessionFac.openSession();
			Doctor d=null;
			Query query=session.createQuery("from Doctor where doctorname=? and doctorpwd=?");
			query.setString(0, doctor.getDoctorname());
			query.setString(1, doctor.getDoctorpwd());
			@SuppressWarnings("unchecked")
			List<Doctor> list=query.list();
			if(list.size()==1){
				d=list.get(0);
			}
			return d;
		}
		//查询所有医生
		public List<Doctor> selectAllDoctor(){
			session=sessionFac.openSession();
			Query query=session.createQuery("from Doctor ");
			@SuppressWarnings("unchecked")
			List<Doctor> list=query.list();
			return list;
		}
		//查询一个医生
		public Doctor selectOneDoctor(Doctor doctor){
			Doctor d=null;
			
			session=sessionFac.openSession();
			Query query=session.createQuery("from Doctor where doctorid=?");
			query.setInteger(0, doctor.getDoctorid());
			@SuppressWarnings("unchecked")
			List<Doctor> list=query.list();
			if(list!=null){
				d=list.get(0);
			}
			return d;
		}
		//通过id号查询一个医生
		public Doctor selectOneDoctorByid(int doctorid){
			Doctor d=null;
			@SuppressWarnings("resource")
			ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
			sessionFac=(SessionFactory)ac.getBean("sessionFac");
			session=sessionFac.openSession();
			Query query=session.createQuery("from Doctor where doctorid=?");
			query.setInteger(0, doctorid);
			@SuppressWarnings("unchecked")
			List<Doctor> list=query.list();
			if(list!=null){
				d=list.get(0);
			}
			return d;
		}
		//添加医生
		public boolean addDoctor(Doctor doctor){
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			boolean bool=false;
			try {
				session.save(doctor);
				tr.commit();
				bool=true;
				session.close();
				return bool;
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return bool;
			}
			
		}
		//更新新闻
		public boolean updateDoctor(Doctor doctor){
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			boolean bool=false;
			try {
				session.update(doctor);
				tr.commit();
				bool=true;
				session.close();
				return bool;
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return bool;
			}
			
		}
		//删除新闻
		public boolean deleteDoctor(Doctor doctor){
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			boolean bool=false;
			try {
				session.delete(doctor);
				tr.commit();
				bool=true;
				session.close();
				return bool;
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return bool;
			}
			
		}
}
